package com.yutel.core;

import org.apache.log4j.Logger;

import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
import com.yutel.system.struts.action.SysUserAction;
/**
 * Session拦截器，判断Session是否过期
 * @author samueli
 */
@SuppressWarnings("serial")
public class SessionFilter extends AbstractInterceptor{
	Logger log = Logger.getLogger(SessionFilter.class);
	@Override
	public String intercept(ActionInvocation invoc) throws Exception {
		ActionSupport action = (ActionSupport) invoc.getAction();
		
		String ns = invoc.getProxy().getNamespace();
		String actionName = invoc.getProxy().getActionName();
		String methodName = invoc.getProxy().getMethod();
		
		if(log.isInfoEnabled()){
			log.info("执行" + ns + actionName + "的" + methodName + "方法");
		}
		if(action instanceof SysUserAction){
			if("login".equalsIgnoreCase(methodName)){
				return invoc.invoke();
			}
		}
		
		Object obj = invoc.getInvocationContext().getSession().get("user");
		if(obj == null){
			log.error("用户无效或者Session过期，将返回登录页面");
			action.addActionError("用户无效或者Session过期");
			return ActionSupport.INPUT;
		}else{
			return invoc.invoke();
		}
	}

}
